package com.utils.generalConfigType.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.utils.generalConfigType.pojo.GeneralConfigType;
import com.utils.generalConfigType.mapper.GeneralConfigTypeMapper;
import com.utils.generalConfigType.service.GeneralConfigTypeService;
import com.utils.common.ResponseEnum;
import com.utils.common.ServerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* 描述:
* author: srdl
* date: 2022-01-05
*/

@Service
public class GeneralConfigTypeServiceImpl implements GeneralConfigTypeService {

    private static Logger logger = LoggerFactory.getLogger(GeneralConfigTypeServiceImpl.class);

    @Autowired
    private GeneralConfigTypeMapper generalConfigTypeMapper ;


    @Override
    public ServerResponse list() {
        try {
            List<GeneralConfigType> jsonObject = generalConfigTypeMapper.list();
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(jsonObject);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).data(e);
        }
    }

    @Override
    public ServerResponse pageList(PageInfo pageInfo) {
        Map<String,Object> returnMap=new HashMap<>();
        try {
            PageHelper.startPage(pageInfo);
            List<GeneralConfigType> list = generalConfigTypeMapper.list();
            PageInfo<GeneralConfigType> jsonObject=new PageInfo<GeneralConfigType>(list);
            returnMap.put("list",jsonObject.getList());
            returnMap.put("pages",jsonObject.getPages());
            returnMap.put("total",jsonObject.getTotal());
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(returnMap);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).data(e);
        }
    }

    @Override
    public ServerResponse get(String id) {
        try {
            GeneralConfigType jsonObject = generalConfigTypeMapper.get(id);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(jsonObject);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).data(e);
        }
    }

    @Override
    public ServerResponse addGeneralConfigType(GeneralConfigType generalConfigType) {
        try {
            generalConfigType.setId(UUID.randomUUID().toString());
            generalConfigTypeMapper.addGeneralConfigType(generalConfigType);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.ADD_SUCCESS).data(generalConfigType);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("添加失败");
        }
    }

    @Override
    public ServerResponse deleteGeneralConfigType(String id) {
        try {
            generalConfigTypeMapper.deleteGeneralConfigType(id);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.DELETE_SUCCESS);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("删除失败");
        }
    }
    @Override
    public ServerResponse updateGeneralConfigType(GeneralConfigType generalConfigType) {
        try{
            generalConfigTypeMapper.updateGeneralConfigType(generalConfigType);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.UPDATE_SUCCESS).data(generalConfigType);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("修改失败");
        }
    }

}
